﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ASoft.Data;

namespace ASoft.WPF.View
{
    /// <summary>
    /// UsersView.xaml 的交互逻辑
    /// </summary>
    public partial class UsersView : Window
    {
        public UsersView()
        {
            InitializeComponent();

            if (RC.Current.IsSysAdmin)
            {
                tabControl.Items.Remove(tabItemChangePassword);
            }
            if (!RC.Current.IsAdmin)
            {
                tabControl.Items.Remove(tabItemUsers);
            }
            ReloadList();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var user = RC.Current.RootData.Users
                .FirstOrDefault(p => string.Equals(p.Code, RC.Current.LoginUserCode, StringComparison.CurrentCultureIgnoreCase));
            if (user == null)
            {
                txtMsg.Text = "没有找到用户.";
                return;
            }
            try
            {
                user.ChangedPassword(txtOldPassword.Password, txtNewPassword.Password, txtSecondPassword.Password);
                txtMsg.Text = "修改成功";
            }
            catch (Exception ex)
            {
                txtMsg.Text = ex.Message;
            }
        }

        private void btnAdd_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var frm = new AddUserView(null);
            frm.ShowDialog();
            if (frm.IsDataChanged)
            {
                ReloadList();
            }
        }

        private void ReloadList()
        {
            listUsers.ItemsSource = null;
            listUsers.ItemsSource = RC.Current.RootData.Users;
            listUsers.SelectedIndex = -1;
            if (RC.Current.RootData.Users.Count > 0)
            {
                listUsers.SelectedIndex = 0;
            }
        }

        private void btnUpdate_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var user = listUsers.SelectedItem as UserData;
            if (user != null)
            {
                var frm = new AddUserView(user);
                frm.ShowDialog();
                if (frm.IsDataChanged)
                {
                    ReloadList();
                }
            }
        }

        private void btnDelete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var user = listUsers.SelectedItem as UserData;
            if (user != null)
            {
                RC.Current.RootData.Users.Remove(user);
                RC.Current.SaveRootData();
                ReloadList();
            }
        }

        private void listUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var user = listUsers.SelectedItem as UserData;
            var enable = user != null;
            btnUpdate.IsEnabled = enable;
            btnDelete.IsEnabled = enable;
        }
    }
}
